package cn.edu.hizee.gps.dao;

import cn.edu.hizee.gps.model.User;
import cn.edu.hizee.gps.model.UserDevice;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.io.Serializable;

/**
 * @author ZX
 */
public interface UserDeviceRepository extends JpaRepository<UserDevice,Integer>, CrudRepository<UserDevice,Integer> {


    /**
     * 查询用户对应的设备号
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "t_device.device_number " +
            "FROM " +
            "t_device " +
            "JOIN t_user_device ON t_user_device.user_id = :userId " +
            "AND t_device.id = t_user_device.device_id ")
    String findDevNum(@Param("userId") String userId);


    /**
     * 查询s设备对应的用户
     * @param devNum
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "t_user.id  " +
            "FROM " +
            "t_user " +
            "JOIN t_device ON t_device.device_number = :devNum " +
            "JOIN t_user_device ON t_user_device.device_id = t_device.id " +
            "AND t_user.id = t_user_device.user_id")
    Integer findUserId(@Param("devNum") String devNum);
}
